package com.sunricher.telinkblemesh.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.sunricher.telinkblemesh.R;
import com.sunricher.telinkblemeshlib.MeshCommand;
import com.sunricher.telinkblemeshlib.MeshManager;

import java.util.ArrayList;
import java.util.List;

public class DaliActivity extends AppCompatActivity {

    private int gatewayAddress = 0;
    private List<Button> setButtons;
    private List<Button> delButtons;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dali);

        gatewayAddress = getIntent().getIntExtra("gatewayAddress", 0);

        Button devicesBtn = findViewById(R.id.devices_btn);
        devicesBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DaliActivity.this, DaliDevicesActivity.class);
                intent.putExtra("gatewayAddress", gatewayAddress);
                startActivity(intent);
            }
        });

        Button groupsBtn = findViewById(R.id.groups_btn);
        groupsBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DaliActivity.this, DaliGroupsActivity.class);
                intent.putExtra("gatewayAddress", gatewayAddress);
                startActivity(intent);
            }
        });

        Button scenesBtn = findViewById(R.id.scenes_btn);
        scenesBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DaliActivity.this, DaliScenesActivity.class);
                intent.putExtra("gatewayAddress", gatewayAddress);
                startActivity(intent);
            }
        });

        Button commandsBtn = findViewById(R.id.commands_btn);
        commandsBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        setButtons = new ArrayList<>();
        setButtons.add(findViewById(R.id.set_1_btn));
        setButtons.add(findViewById(R.id.set_2_btn));
        setButtons.add(findViewById(R.id.set_3_btn));
        setButtons.add(findViewById(R.id.set_4_btn));
        delButtons = new ArrayList<>();
        delButtons.add(findViewById(R.id.del_1_btn));
        delButtons.add(findViewById(R.id.del_2_btn));
        delButtons.add(findViewById(R.id.del_3_btn));
        delButtons.add(findViewById(R.id.del_4_btn));

        setButtons.get(0).setOnClickListener(v -> {
            String position = MeshCommand.SmartSwitchActions.ButtonPosition.key4TopLeft;
            setSmartSwitchShortPress(position);
            setSmartSwitchLongPressBegin(position);
            setSmartSwitchLongPressEnd(position);
        });
        setButtons.get(1).setOnClickListener(v -> {
            String position = MeshCommand.SmartSwitchActions.ButtonPosition.key4TopRight;
            setSmartSwitchShortPress(position);
            setSmartSwitchLongPressBegin(position);
            setSmartSwitchLongPressEnd(position);
        });
        setButtons.get(2).setOnClickListener(v -> {
            String position = MeshCommand.SmartSwitchActions.ButtonPosition.key4BottomLeft;
            setSmartSwitchShortPress(position);
            setSmartSwitchLongPressBegin(position);
            setSmartSwitchLongPressEnd(position);
        });
        setButtons.get(3).setOnClickListener(v -> {
            String position = MeshCommand.SmartSwitchActions.ButtonPosition.key4BottomRight;
            setSmartSwitchShortPress(position);
            setSmartSwitchLongPressBegin(position);
            setSmartSwitchLongPressEnd(position);
        });

        delButtons.get(0).setOnClickListener(v -> {
            String position = MeshCommand.SmartSwitchActions.ButtonPosition.key4TopLeft;
            MeshCommand delete = MeshCommand.UartDali.deleteSmartSwitch(this.gatewayAddress, position, 0x7000000D);
            MeshManager.getInstance().send(delete);
        });

        delButtons.get(1).setOnClickListener(v -> {
            String position = MeshCommand.SmartSwitchActions.ButtonPosition.key4TopRight;
            MeshCommand delete = MeshCommand.UartDali.deleteSmartSwitch(this.gatewayAddress, position, 0x7000000D);
            MeshManager.getInstance().send(delete);
        });

        delButtons.get(2).setOnClickListener(v -> {
            String position = MeshCommand.SmartSwitchActions.ButtonPosition.key4BottomLeft;
            MeshCommand delete = MeshCommand.UartDali.deleteSmartSwitch(this.gatewayAddress, position, 0x7000000D);
            MeshManager.getInstance().send(delete);
        });

        delButtons.get(3).setOnClickListener(v -> {
            String position = MeshCommand.SmartSwitchActions.ButtonPosition.key4BottomRight;
            MeshCommand delete = MeshCommand.UartDali.deleteSmartSwitch(this.gatewayAddress, position, 0x7000000D);
            MeshManager.getInstance().send(delete);
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        MeshManager.getInstance().setUartDaliGateway(gatewayAddress);
    }

    @Override
    protected void onStop() {
        super.onStop();
        MeshManager.getInstance().resetUartDaliGateway();
    }

    private void setSmartSwitchShortPress(String buttonPosition) {
        // 初始化 Target, Action
        MeshCommand.UartDali.DaliAction.Target target = new MeshCommand.UartDali.DaliAction.Target(MeshCommand.UartDali.DaliAction.Target.TargetType.broadcast);
        MeshCommand.UartDali.DaliAction.Action action = new MeshCommand.UartDali.DaliAction.Action(MeshCommand.UartDali.DaliAction.Action.ActionType.none);
        switch (buttonPosition) {
            // 左上角按键
            case MeshCommand.SmartSwitchActions.ButtonPosition.key4TopLeft: {
                // 动作更新为 off
                action.setActionType(MeshCommand.UartDali.DaliAction.Action.ActionType.off);
                break;
            }
            // 右上角按键
            case MeshCommand.SmartSwitchActions.ButtonPosition.key4TopRight: {
                // 目标更新为 单设备 地址 2
                target.setTargetType(MeshCommand.UartDali.DaliAction.Target.TargetType.device);
                target.setDeviceAddress(0x02);
                // 动作更新为 调用场景 0
                action.setActionType(MeshCommand.UartDali.DaliAction.Action.ActionType.goToScene);
                action.setSceneId(0);
                break;
            }
            // 左下角按键
            case MeshCommand.SmartSwitchActions.ButtonPosition.key4BottomLeft: {
                // 动作更新为 off
                action.setActionType(MeshCommand.UartDali.DaliAction.Action.ActionType.off);
                break;
            }
            // 右下角按键
            case MeshCommand.SmartSwitchActions.ButtonPosition.key4BottomRight: {
                // 动作更新为 亮度 254
                action.setActionType(MeshCommand.UartDali.DaliAction.Action.ActionType.level);
                action.setLevel(254);
                break;
            }
            default:
                break;
        }
        MeshCommand.UartDali.DaliAction daliAction = new MeshCommand.UartDali.DaliAction(target, action);
        List<MeshCommand> commands = MeshCommand.UartDali.setSmartSwitch(this.gatewayAddress, 0x7000000D, buttonPosition, MeshCommand.SmartSwitchActions.ButtonEvent.shortPress, daliAction);
        MeshManager.getInstance().sendCommands(commands, 500);
    }

    private void setSmartSwitchLongPressBegin(String buttonPosition) {
        // 初始化 Target, Action
        MeshCommand.UartDali.DaliAction.Target target = new MeshCommand.UartDali.DaliAction.Target(MeshCommand.UartDali.DaliAction.Target.TargetType.broadcast);
        MeshCommand.UartDali.DaliAction.Action action = new MeshCommand.UartDali.DaliAction.Action(MeshCommand.UartDali.DaliAction.Action.ActionType.none);
        switch (buttonPosition) {
            // 左上角按键
            case MeshCommand.SmartSwitchActions.ButtonPosition.key4TopLeft: {
                // 动作更新为 level 125
                action.setActionType(MeshCommand.UartDali.DaliAction.Action.ActionType.level);
                action.setLevel(125);
                break;
            }
            // 右上角按键
            case MeshCommand.SmartSwitchActions.ButtonPosition.key4TopRight: {
                // 目标更新为 单设备 地址 2
                target.setTargetType(MeshCommand.UartDali.DaliAction.Target.TargetType.device);
                target.setDeviceAddress(0x02);
                // 动作更新为 调用场景 0
                action.setActionType(MeshCommand.UartDali.DaliAction.Action.ActionType.level);
                action.setLevel(254);
                break;
            }
            // 左下角按键
            case MeshCommand.SmartSwitchActions.ButtonPosition.key4BottomLeft: {
                // 动作更新为 none
                action.setActionType(MeshCommand.UartDali.DaliAction.Action.ActionType.none);
                break;
            }
            // 右下角按键
            case MeshCommand.SmartSwitchActions.ButtonPosition.key4BottomRight: {
                // 动作更新为 none
                action.setActionType(MeshCommand.UartDali.DaliAction.Action.ActionType.none);
                break;
            }
            default:
                break;
        }
        MeshCommand.UartDali.DaliAction daliAction = new MeshCommand.UartDali.DaliAction(target, action);
        List<MeshCommand> commands = MeshCommand.UartDali.setSmartSwitch(this.gatewayAddress, 0x7000000D, buttonPosition, MeshCommand.SmartSwitchActions.ButtonEvent.longPressBegin, daliAction);
        MeshManager.getInstance().sendCommands(commands, 500);
    }

    private void setSmartSwitchLongPressEnd(String buttonPosition) {
        // 初始化 Target, Action
        MeshCommand.UartDali.DaliAction.Target target = new MeshCommand.UartDali.DaliAction.Target(MeshCommand.UartDali.DaliAction.Target.TargetType.broadcast);
        MeshCommand.UartDali.DaliAction.Action action = new MeshCommand.UartDali.DaliAction.Action(MeshCommand.UartDali.DaliAction.Action.ActionType.none);
        switch (buttonPosition) {
            // 左上角按键
            case MeshCommand.SmartSwitchActions.ButtonPosition.key4TopLeft: {
                // 动作更新为 level 254
                action.setActionType(MeshCommand.UartDali.DaliAction.Action.ActionType.level);
                action.setLevel(254);
                break;
            }
            // 右上角按键
            case MeshCommand.SmartSwitchActions.ButtonPosition.key4TopRight:
            // 左下角按键
            case MeshCommand.SmartSwitchActions.ButtonPosition.key4BottomLeft:
            // 右下角按键
            case MeshCommand.SmartSwitchActions.ButtonPosition.key4BottomRight: {
                // 动作更新为 none
                action.setActionType(MeshCommand.UartDali.DaliAction.Action.ActionType.none);
                break;
            }
            default:
                break;
        }
        MeshCommand.UartDali.DaliAction daliAction = new MeshCommand.UartDali.DaliAction(target, action);
        List<MeshCommand> commands = MeshCommand.UartDali.setSmartSwitch(this.gatewayAddress, 0x7000000D, buttonPosition, MeshCommand.SmartSwitchActions.ButtonEvent.longPressEnd, daliAction);
        MeshManager.getInstance().sendCommands(commands, 500);
    }
}